enum TOOLBARS {
    //% block="隐藏"
    "None",
    //% block="打开"
    "OPEN",
}

enum FONTCOLOR {
    //% block="黑色"
    "black",
    //% block="蓝色"
    "blue",
    //% block="紫色"
    "purple",
    //% block="红色"
    "red",
    //% block="灰色"
    "grey",
    //% block="白色"
    "white",
    //% block="绿色"
    "green",
    //% block="粉色"
    "pink",
    //% block="橙色"
    "orange",
    
}


//% color="#ef784c" iconWidth=50 iconHeight=40
namespace Unihiker_Matplotlib{

    //% block="Matplotlib模块初始化" blockType="command"
    export function matplotlib_init(parameter: any, block: any) {
        Generator.addImport(`import os`);
        Generator.addImport(`from matplotlib import pyplot as plt`);
    }

    //% block="设置图表[TOOLBAR]工具栏" blockType="command"
    //% TOOLBAR.shadow="dropdown" TOOLBAR.options="TOOLBARS"
    export function toolbar_show(parameter: any, block: any) {
        let toolbar=parameter.TOOLBAR.code;
        if (toolbar.string=="None"){
            Generator.addCode(`plt.rcParams['toolbar'] = 'None'`)
        }
    }

    //% block="设置图表显示字体[FONTTYPE]" blockType="command"
    //% FONTTYPE.shadow="string" FONTTYPE.defl="HYQiHei"
    export function font_type(parameter: any, block: any) {
        let fonttype=parameter.FONTTYPE.code;
        Generator.addCode(`plt.rcParams['font.sans-serif'] = ${fonttype}`)
    }

    //% block="清除当前轴内容" blockType="command"
    export function plt_cla(parameter: any, block: any) {
        Generator.addCode(`plt.cla()`)
    }

    //% block="清除窗口" blockType="command"
    export function plt_clf(parameter: any, block: any) {
        Generator.addCode(`plt.clf()`)
    }

    //% block="设置图表显示字体大小[FONTSIZE]" blockType="command"
    //% FONTSIZE.shadow="number" FONTSIZE.defl="5"
    export function font_size(parameter: any, block: any) {
        let fontsize=parameter.FONTSIZE.code;
        Generator.addCode(`plt.rcParams['font.size'] = ${fontsize}`)
    }

    //% block="创建画图区域宽[WIDTH]高[HEIGH]并返回画布对象[FIG]轴对象[AXES]" blockType="command"
    //% WIDTH.shadow="number" WIDTH.defl="240"
    //% HEIGH.shadow="number" HEIGH.defl="320"
    //% FIG.shadow="string" FIG.defl="fig"
    //% AXES.shadow="string" AXES.defl="axes"
    export function create_axes(parameter: any, block: any) {
        let width=parameter.WIDTH.code;
        let heigh=parameter.HEIGH.code;
        let fig=parameter.FIG.code;
        let axes=parameter.AXES.code;
        Generator.addCode(`fig, axes = plt.subplots(1,1,figsize=(${width}/100,${heigh}/100))`)
    }

    //% block="对象名[AXES]标题[TITLE]字体大小[SIZE]颜色[COLOR]" blockType="command"
    //% AXES.shadow="string" AXES.defl="axes"
    //% TITLE.shadow="string" TITLE.defl="标题内容"
    //% SIZE.shadow="number" SIZE.defl="10"
    //% COLOR.shadow="dropdown" COLOR.options="FONTCOLOR" COLOR.defl="FONTCOLOR.黑色"
    export function set_axes(parameter: any, block: any) {
        let axes=parameter.AXES.code;
        let title=parameter.TITLE.code;
        let size=parameter.SIZE.code;
        let color=parameter.COLOR.code;
        Generator.addCode(`axes.set_title(${title},color=${color},size="${size}")`)
    }

    //% block="列表数据开始[START_NUM]结束[END_NUM]步长[STEP]" blockType="reporter"
    //% START_NUM.shadow="number" START_NUM.defl="0"
    //% END_NUM.shadow="number" END_NUM.defl="10"
    //% STEP.shadow="number" STEP.defl="1"
    export function num_range(parameter: any, block: any) {
        let start_num=parameter.START_NUM.code;
        let end_num=parameter.END_NUM.code;
        let step=parameter.STEP.code;
        Generator.addCode(`list(range(${start_num}, ${end_num}, ${step})`)
    }

    //% block="设置Y轴刻度[START][END]" blockType="command"
    //% START.shadow="number" START.defl="0"
    //% END.shadow="number" END.defl="100"
    export function set_yticks(parameter: any, block: any) {
        let start=parameter.START.code;
        let end=parameter.END.code;
        Generator.addCode(`plt.ylim(${start}, ${end})`)
    }

    //% block="绘制折线图[DATA]" blockType="command"
    //% DATA.shadow="normal" DATA.defl="['数据1','数据2']"
    export function plot(parameter: any, block: any) {
        let data=parameter.DATA.code;
        Generator.addCode(`plt.plot(${data})`)
    }

    //% block="绘制折线图,X列表[X_DATA],Y列表[Y_DATA]" blockType="command"
    //% X_DATA.shadow="normal" X_DATA.defl="['11-01','11-02','11-03']"
    //% Y_DATA.shadow="normal" Y_DATA.defl="['12','15','10']"
    export function plot(parameter: any, block: any) {
        let x_data=parameter.X_DATA.code;
        let y_data=parameter.Y_DATA.code;
        Generator.addCode(`plt.plot(${x_data}, ${y_data})`)
    }


    //% block="显示图例" blockType="command"
    export function legend(parameter: any, block: any) {
        Generator.addCode(`plt.legend()`)
    }

    //% block="显示图像" blockType="command"
    export function show(parameter: any, block: any) {
        Generator.addCode(`plt.show()`)
    }

    //% block="图像保存为[FILENAME]" blockType="command"
    //% FILENAME.shadow="string" FILENAME.defl="image.png"
    export function savefig(parameter: any, block: any) {
        let filepath=parameter.FILENAME.code;
        Generator.addCode(`
if os.path.exists(${filepath}):
    os.remove(${filepath})       
`)
        Generator.addCode(`plt.savefig(${filepath})`)
    }

    //% block="列表[LIST]最大值" blockType="reporter"
    //% LIST.shadow="normal" FILENAME.defl="[1,2,3,4,5]"
    export function list_max(parameter: any, block: any) {
        let datas=parameter.LIST.code;
        Generator.addCode(`max([i for i in ${datas}])`)
    }

    //% block="计算动摩擦力[LIST]" blockType="reporter"
    //% LIST.shadow="normal" FILENAME.defl="[1,2,3,4,5]"
    export function list_main(parameter: any, block: any) {
        let datas=parameter.LIST.code;
        Generator.addImport(`from mocali_calc import moca_calc`);
        Generator.addCode(`moca_calc(${datas})`)
    }

}
